﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace WebHousingInfo.Admin
{
    public partial class Add_Edit_ProjectNews : System.Web.UI.Page
    {
        private static string mode = null, modeap = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                mode = Request.QueryString["mode"];
                modeap = Request.QueryString["type"];
                getcategory();
                if (mode == null)
                {
                    lblxem_duyet.Text = "Thêm mới dự án";
                }
                else
                {
                    lblxem_duyet.Text = "Chỉnh sửa thông tin dự án";
                    ProjectBO projectbo = ProjectBO.GetProjectBO(Convert.ToInt32(mode));
                    txtname.Text = projectbo.Name.Trim();
                    dlloaiduan.SelectedValue = projectbo.CategoryID.ToString();
                    txtmota.Text = projectbo.Description.Trim();
                    txtaddress.Text = projectbo.Address.Trim();
                    txtsure.Text = projectbo.Sure.Trim();
                    txtsotang.Text = projectbo.NumberFloor.Trim();
                    txtdautu.Text = projectbo.Investors.Trim();
                    txtthicong.Text = projectbo.Construction.Trim();
                    txtthietke.Text = projectbo.Design.Trim();
                    txtquimo.Text = projectbo.Scale.Trim();
                    txtchitiet.Text = projectbo.ContentProject.Trim();
                    cbkichhoat.Checked = projectbo.IsActive;
                    btnxoa.Visible = true;
                    btnketthuc.Visible = true;
                    btndangtin.Visible = false;
                    btncapnhat.Visible = true;
                    if(modeap == "approve")
                    {
                        btnketthuc.Visible = false;
                        btnduyet.Visible = true;
                        btncapnhat.Visible = false;
                    }
                }
            }
        }

        public void getcategory()
        {
            try
            {
                var category = CategoryInfo.GetCategoryInfo_BySingle(false, false, true, false, true);
                dlloaiduan.DataSource = category;
                dlloaiduan.DataBind();
            }
            catch { }
        }

        protected void btndangtin_Click(object sender, EventArgs e)
        {
            ProjectBO project = ProjectBO.NewProjectBO();
            if (Session["UserNameAdmin"] == null)
                Response.Redirect("Login.aspx");
            else
                project.UserName = Session["UserNameAdmin"].ToString();
            project.Name = txtname.Text.Trim();
            project.CategoryID = Convert.ToInt32(dlloaiduan.SelectedValue);
            project.Description = txtmota.Text.Trim();
            project.CreatedDate = DateTime.Now;
            project.Address = txtaddress.Text.Trim();
            project.Sure = txtsure.Text.Trim();
            project.NumberFloor = txtsotang.Text.Trim();
            project.Investors = txtdautu.Text.Trim();
            project.Construction = txtthicong.Text.Trim();
            project.Design = txtthietke.Text.Trim();
            project.Scale = txtquimo.Text.Trim();
            project.ContentProject = txtchitiet.Text.Trim();
            project.IsActive = cbkichhoat.Checked;
            try
            {
                project.Save();
                Response.Redirect("Project.aspx?type=insert");
            }
            catch
            {
                pn_result.Visible = true;
                lblthongbao.Text = "Thêm thất bại. Vui lòng thử lại !";
            }
        }

        protected void btnketthuc_Click(object sender, EventArgs e)
        {
            ProjectBO project = ProjectBO.GetProjectBO(Convert.ToInt32(mode));
            project.IsActive = false;
            try
            {
                project.Save();
                Response.Redirect("Project.aspx?type=end");
            }
            catch
            {
                pn_result.Visible = true;
                lblthongbao.Text = "Kết thúc đăng dự án thất bại. Vui lòng thử lại!";
            }
        }

        protected void btnxoa_Click(object sender, EventArgs e)
        {
            try
            {
                ProjectBO.DeleteProjectBO(Convert.ToInt32(mode));
                if (modeap == "approve")
                    Response.Redirect("Project.aspx?mode=approve&type=delete");
                else Response.Redirect("Project.aspx?type=delete");
            }
            catch {
                pn_result.Visible = true;
                lblthongbao.Text = "Xóa thông tin dự án thất bại. Vui lòng thử lại !";
            }
        }

        protected void btnhuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("Project.aspx");
        }

        protected void btnduyet_Click(object sender, EventArgs e)
        {
            ProjectBO project = ProjectBO.GetProjectBO(Convert.ToInt32(mode));
            project.IsActive = true;
            try
            {
                project.Save();
                Response.Redirect("Project.aspx?mode=approve&type=approve");
            }
            catch
            {
                pn_result.Visible = true;
                lblthongbao.Text = "Duyệt dự án thất bại. Vui lòng thử lại!";
            }
        }

        protected void btncapnhat_Click(object sender, EventArgs e)
        {
            ProjectBO project = ProjectBO.GetProjectBO(Convert.ToInt32(mode));
            project.Name = txtname.Text;
            project.CategoryID = Convert.ToInt32(dlloaiduan.SelectedValue);
            project.Description = txtmota.Text;
            project.Address = txtaddress.Text;
            project.Sure = txtsure.Text;
            project.NumberFloor = txtsotang.Text;
            project.Investors = txtdautu.Text;
            project.Construction = txtthicong.Text;
            project.Design = txtthietke.Text;
            project.Scale = txtquimo.Text;
            project.ContentProject = txtchitiet.Text;
            project.IsActive = cbkichhoat.Checked;
            try
            {
                project.Save();
                Response.Redirect("Project.aspx?type=update");
            }
            catch
            {
                pn_result.Visible = true;
                lblthongbao.Text = "Thêm dự án thất bại. Vui lòng thử lại !";
            }
        }
    }
}
